package com.neusoft.neuiotms.dcam.fixed.service.spareconsume.impl;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.fixed.converter.spareconsume.SpareConsumeApproveConver;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeApproveDTO;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeApprove;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeApproveRepository;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeApproveService;

@Service
public class SpareConsumeApproveServiceImpl implements SpareConsumeApproveService {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Autowired
    private SpareConsumeApproveRepository spareConsumeApproveRepository;

    @Autowired
    private SpareConsumeApproveConver spareConsumeApproveConver;

    @Override
    @Transactional
    public SpareConsumeApproveDTO create(final SpareConsumeApproveDTO dto, final UserBean user) {
        // 设置当前节点操作人
        dto.setOperatorCode(user.getUsername());
        dto.setOperatorName(user.getName());

        String value = "";
        if (dto.getWhetherAgree()) {
            value = "Agree";
        } else {
            value = "Reject";
        }
        // 设置流程变量是否同意
        // this.processInstanceService.setGlobalVariable(dto.getTaskId(),
        // DCAMConsts.SPARECONSUME_PROCESS_LEADER_OR_SUPERVISOR_AGREES, dto.getWhetherAgree());
        this.processInstanceService.setGlobalVariable(dto.getTaskId(),
                "passFlag", value);

        // 完成节点
        this.processInstanceService.complete(dto.getWorkNum(), dto.getTaskId(), user);

        // 查询当前workNum和TaskId下是否存在记录
        final SpareConsumeApprove oldModel =
                this.spareConsumeApproveRepository.findByWorkNumAndTaskId(
                        dto.getWorkNum(), dto.getTaskId());

        // 如果不存在新增
        if (oldModel == null) {
            dto.setId(null);
        }

        // 保存操作信息
        final SpareConsumeApprove model = this.spareConsumeApproveConver.toModel(dto);
        this.spareConsumeApproveRepository.save(model);
        return this.spareConsumeApproveConver.toDTO(model);
    }

    @Override
    public SpareConsumeApproveDTO detail(final String workNum, final String taskId, final UserBean user) {
        SpareConsumeApprove model = null;
        try {
            if (StringUtils.isNotBlank(workNum) && !"NULL".equals(taskId)) {
                model = this.spareConsumeApproveRepository.findByWorkNumAndTaskId(workNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new SpareConsumeApproveDTO();
                    } else {
                        model = this.spareConsumeApproveRepository.findByWorkNumAndTaskId(workNum, fromProcessTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.spareConsumeApproveRepository.findTopByWorkNumOrderByIdDesc(workNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workNum },
                    new Object[] {taskId });
        }
        return this.spareConsumeApproveConver.toDTO(model);

    }

}
